import 'dart:convert';

import 'package:core_package/core_package.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/services.dart';
import 'package:flutter_app_unionad/core/configuration.dart';
import 'package:flutter_app_unionad/model/cash_detail_list_item.dart';
import 'package:flutter_app_unionad/model/device_info_model.dart';
import 'package:flutter_app_unionad/model/exchange_list_model.dart';
import 'package:flutter_app_unionad/model/my_device_list_model.dart';
import 'package:flutter_app_unionad/model/pay_rule_model.dart';
import 'package:flutter_app_unionad/model/province_model.dart';
import 'package:flutter_app_unionad/model/recharge_order_model.dart';
import 'package:flutter_app_unionad/model/user_device_list_model.dart';
import 'package:flutter_app_unionad/request/base_request.dart';

class FacilityRequest {
  //获取省市县
  static Future<List<ProvinceModel>> getAllProvince(BuildContext context) async {
    final future = await rootBundle.loadString("assets/file/chinese_cities.json");
    List jsonList = json.decode(future);
    List<ProvinceModel> list = [];
    for (var map in jsonList) {
      list.add(ProvinceModel.fromJson(map));
    }
    return list;
  }

  /*
  * Base64加密
  */
  static String encodeBase64(String data) {
    var content = utf8.encode(data);
    var digest = base64Encode(content);
    return digest;
  }

  //预约安装
  static Future<bool> makeAnAppointment({
    @required int? sex,
    @required String? userAddress,
    @required String? userTel,
    @required String? userName,
    @required String? city,
    @required String? userCityCode,
    @required String? userProvince,
    @required String? userProvinceCode,
    @required String? userCounty,
    @required String? userCountyCode,
    String? invitePhone,
  }) async {
    if (invitePhone == null || invitePhone.stringIsEmpty) {
      invitePhone = UserCacheUil.getString(Configuration.mobile);
    }
    Map<String, dynamic> map = {
      'sex': sex,
      'userAddress': userAddress,
      'userTel': userTel,
      'userName': userName,
      'userCityCode': userCityCode,
      'city': city,
      'userProvince': userProvince,
      'userProvinceCode': userProvinceCode,
      'userCounty': userCounty,
      'userCountyCode': userCountyCode,
      'invitePhone': encodeBase64(invitePhone!),
    };

    final future = await HttpRequest.postRequest("/api/booking/add2", params: map);
    bool result = false;
    baseRequestAnalysisData(future, (map) {
      result = true;
    }, (info, code) {
      result = false;
      ShowToastWidget.showToast(msg: info);
    });
    return result;
  }

  //获取设备信息
  static Future<DeviceInfoModel?> getDevice({@required String? device}) async {
    if (device == null || device.stringIsEmpty) return null;
    final future = await HttpRequest.getRequest('/api/device/getDevInfo', params: {'deviceId': device});
    DeviceInfoModel? model;
    baseRequestAnalysisData(future, (map) {
      //
      model = DeviceInfoModel.fromJson(map);
    }, (info, code) {
      //
    });
    return model;
  }

  //设备充值规则列表 type  类型 用户类型  01-家庭按天 02-商用按天 03-商用按流量 04-合租按流量
  static Future<List<PayRuleModel>?> payRuleList({String? devID}) async {
    final future = await HttpRequest.getRequest('/api/payment/payRuleList', params: {'devId': devID});

    List<PayRuleModel>? list;
    baseRequestAnalysisData(future, (map) {
      //

      list = [];
      for (var m in map) {
        list?.add(PayRuleModel.fromJson(m));
      }
    }, (info, code) {
      //
    });
    return list;
  }

  //支付宝支付
  static Future<String?> aliAppPay({
    @required num? amount,
    @required String? devId,
    @required String? openId,
    @required String? payRuleId,
  }) async {
    final future = await HttpRequest.getRequest(
      '/api/pay/ali/appPay',
      params: {
        'amount': amount,
        'devId': devId,
        'openId': openId,
        'payRuleId': payRuleId,
      },
    );
    String? str;
    baseRequestAnalysisData(future, (map) {
      str = map['appPayResponse'];
    }, (info, code) {
      ShowToastWidget.showToast(msg: info);
    });
    return str;
  }

  //微信支付
  static Future<Map?> weChatAppPay({
    @required num? amount,
    @required String? devId,
    @required String? openId,
    @required String? payRuleId,
  }) async {
    final future = await HttpRequest.getRequest(
      '/api/pay/wx/appPay',
      params: {
        'amount': amount,
        'devId': devId,
        'openId': openId,
        'payRuleId': payRuleId,
      },
    );
    Map? map1;
    print(future);
    baseRequestAnalysisData(future, (map) {
      map1 = map;
      print(map);
    }, (info, code) {
      ShowToastWidget.showToast(msg: info);
    });

    return map1;
  }

  ///充值完保存结果
  static Future<bool> rechargeSaveAd({
    required String record,
  }) async {
    final future = await HttpRequest.getRequest(
      '/api/pointRecord/givePointByPaymentDeviceShare',
      params: {
        'payInfoId': record,
      },
    );
    bool res = false;
    print(future);
    baseRequestAnalysisData(future, (map) {
      res = true;
    }, (info, code) {
      ShowToastWidget.showToast(msg: info);
    });

    return res;
  }

  static Future<List<RechargeOrderModel>?> getOrderList({
    String? openId,
    int? pageNum = 0,
    String pageSize = '20',
  }) async {
    final future = await HttpRequest.postRequest('/api/payment/orderList', params: {
      'pageNum': pageNum.toString(),
      'pageSize': pageSize,
      'openId': UserCacheUil.getString(Configuration.openid),
    });
    List<RechargeOrderModel>? list;
    baseRequestAnalysisData(future, (map) {
      list = [];
      for (var v in map['list']) {
        list?.add(RechargeOrderModel.fromJson(v));
      }
    }, (info, code) {});
    return list;
  }

  //提现记录
  static Future<List<CashDetailListItem>?> cashDetailList({
    String? openId,
    int? pageNum = 0,
    String pageSize = '20',
  }) async {
    final future = await HttpRequest.postRequest('/api/lingXinPay/selectCashDetailList', params: {
      'pageNum': pageNum,
      'pageSize': pageSize,
    });
    List<CashDetailListItem>? list;
    baseRequestAnalysisData(future, (map) {
      list = [];
      for (var v in map['list']) {
        list?.add(CashDetailListItem.fromJson(v));
      }
    }, (info, code) {});
    return list;
  }

  //兑换记录
  static Future<List<ExchangeListModel>?> exchangeDetailList({
    String? openId,
    int? pageNum = 0,
    String? pageSize = '20',
  }) async {
    final future = await HttpRequest.postRequest('/api/lingXinPay/selectExchangeDaysList', params: {
      'pageNum': pageNum,
      'pageSize': pageSize,
    });
    List<ExchangeListModel>? list;
    baseRequestAnalysisData(future, (map) {
      list = [];
      for (var v in map['list']) {
        list?.add(ExchangeListModel.fromJson(v));
      }
    }, (info, code) {});
    return list;
  }

  //获取用户设备列表
  static Future<UserDeviceListModel?> getUserDeviceListRequest() async {
    final future = await HttpRequest.getRequest('/api/device/getBindDevList');
    UserDeviceListModel? item;
    baseRequestAnalysisData(future, (data) {
      item = UserDeviceListModel.fromJson(data);
    }, (info, code) {});
    return item;
  }

  //获取用户设备列表
  static Future<List<MyDeviceListModel>> getMyUserDeviceList() async {
    final future = await HttpRequest.getRequest('/api/device/myDeviceList');
    List<MyDeviceListModel> list = [];
    baseRequestAnalysisData(future, (data) {
      for (var v in data) {
        list.add(MyDeviceListModel.fromJson(v));
      }
    }, (info, code) {});
    return list;
  }

  //预约安装
  static Future<bool> changeDeviceInfo({
    required int devId,
    @required String? userAddress,
    @required String? userTel,
    @required String? userName,
    @required String? city,
    @required String? userCityCode,
    @required String? userProvince,
    @required String? userProvinceCode,
    @required String? userCounty,
    @required String? userCountyCode,
  }) async {
    Map<String, dynamic> map = {
      'devId':devId,
      'devAddress': userAddress,
      'contractTel': userTel,
      'contractName': userName,
      'devCityCode': userCityCode,
      'devCity': city,
      'devProvince': userProvince,
      'devProvinceCode': userProvinceCode,
      'devCounty': userCounty,
      'devCountyCode': userCountyCode,
    };

    final future = await HttpRequest.postRequest("/api/device/updateDeviceAddressInfo", params: map);
    bool result = false;
    baseRequestAnalysisData(future, (map) {
      result = true;
    }, (info, code) {
      result = false;
      ShowToastWidget.showToast(msg: info);
    });
    return result;
  }
}
